from  pathlib import *
import os
import cv2

def extractFrame(inputfile, savedir):
    video = cv2.VideoCapture(str(inputfile))
    frame_cnt = 0
    extract_rate = 10

    while True:
        flag, frame = video.read()
        if flag:
            if frame_cnt%extract_rate==0:
                cv2.imwrite(str(Path(savedir, "%s_%d.jpg"%(str(inputfile.stem), frame_cnt) )),frame)
            frame_cnt += 1
            

        else:
            break

newdir = "target"
p = Path("sample")
for p in p.iterdir():
    newdirpath = Path(newdir)/p.stem
    newdirpath.mkdir(parents=True, exist_ok=True)
    extractFrame(p, newdirpath)

    